package at.horario.web.property.editor;

import at.horario.model.Celula;
import at.horario.model.Horario;
import at.horario.service.CelulaManager;
import at.horario.web.pages.ProfessorRow;
import org.apache.wicket.model.LoadableDetachableModel;

import java.util.List;


/**
 * A detachable model that can load an Horario object from persistent store.
 *
 * @author ivaynberg
 */
public class ProfessorRowModel extends LoadableDetachableModel {
    private CelulaManager celulaManager;
    private Horario horario;
    private Integer professorNum;

    public ProfessorRowModel(CelulaManager celulaManager) {
        this.celulaManager = celulaManager;
    }

    public ProfessorRowModel(ProfessorRow row, CelulaManager celulaManager) {
        super(row);
        this.horario = row.getHorario();
        this.celulaManager = celulaManager;
        this.professorNum = row.getProfessor();
    }

    protected Object load() {
        List<Celula> celulas = celulaManager.getCelulas(horario, professorNum);
        ProfessorRow pr = new ProfessorRow(horario, professorNum);
        for (int i = 0; i < celulas.size(); i++) {
            Celula cel = celulas.get(i);
            pr.getHorarios()[cel.getNumeroHorario()] = cel.getTurma();
        }

        return pr;
    }

}